﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell;
using EnvDTE80;
using EnvDTE;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;

namespace SD007
{
    public class DetectorMessages 
        //: System.IServiceProvider
    {
        private ErrorListProvider m_objErrorListProvider;
        private List<ErrorTask> m_colErrorTasks;
        private DTE2 _dte2;        

        //public Object GetService(System.Type serviceType)
        //{
        //    Object objService = null;
        //    try 
        //    {
        //        //System.IServiceProvider serviceProvider = 
        //        //    new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)_dte2);
        //        objService = (IVsSolution)_dte2.Solution;
        //        //objService = serviceProvider;
        //    }
        //    catch (Exception)
        //    {   
        //    }
        //    return objService;
        //}

        public DetectorMessages(DTE2 _dte2)
        {
            this._dte2 = _dte2;
            this.InitializeErrorListProvider();
        }

        protected void InitializeErrorListProvider()
        {
            m_colErrorTasks = new List<ErrorTask>();
            System.IServiceProvider serviceProvider =
                new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)_dte2);
            m_objErrorListProvider = new Microsoft.VisualStudio.Shell.ErrorListProvider(serviceProvider);
            m_objErrorListProvider.ProviderName = "SD007 Error Provider";
            m_objErrorListProvider.ProviderGuid = Guid.NewGuid();
            m_objErrorListProvider.Show();
        }

        public void AddError(Project proj, ProjectItem  projItem, string message, int line, int column)
        {
            if(proj != null && projItem != null) 
            {
                AddErrorToErrorList(proj, projItem, message, TaskErrorCategory.Error, line, column);
            }
        }

        protected void AddErrorToErrorList(Project proj, ProjectItem projItem, 
            string message, TaskErrorCategory category, int line, int column)
        {
            ErrorTask objErrorTask;
            Microsoft.VisualStudio.Shell.Interop.IVsSolution objIVsSolution;
            IVsHierarchy objVsHierarchy = null;
            try
            {
                //objIVsSolution = (IVsSolution)GetService(typeof(SVsSolution));
                //objIVsSolution = (IVsSolution)GetService(typeof(SVsSolution));
                //ErrorHandler.ThrowOnFailure(objIVsSolution.GetProjectOfUniqueName(proj.UniqueName, out objVsHierarchy));
                objErrorTask = new Microsoft.VisualStudio.Shell.ErrorTask();
                objErrorTask.ErrorCategory = category;
                objErrorTask.HierarchyItem = objVsHierarchy;
                objErrorTask.Document = projItem.FileNames[0];
                objErrorTask.Line = line;
                objErrorTask.Column = column;
                objErrorTask.Text = message;

                // REVER ESTA LINHA 
                // AddHandler objErrorTask.Navigate, AddressOf ErrorTaskNavigate                

                m_colErrorTasks.Add(objErrorTask);
                m_objErrorListProvider.Tasks.Add(objErrorTask);
            }
            catch (Exception)
            {
            }
        }

        public void RemoveTasks()
        {
            ErrorTask objErrorTask;
            try
            {
                if (m_colErrorTasks != null)
                {
                    for (int i = m_colErrorTasks.Count - 1; i >= 0 ; i--)
                    {
                        objErrorTask = m_colErrorTasks[i];
                        RemoveTask(objErrorTask);
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void RemoveTask(ErrorTask objErrorTask)
        {
            try
            {
                //m_objErrorListProvider.SuspendRefresh();

                // REVER ESTA LINHA
                //RemoveHandler objErrorTask.Navigate, AddressOf ErrorTaskNavigate

                m_colErrorTasks.Remove(objErrorTask);
                m_objErrorListProvider.Tasks.Remove(objErrorTask);
                m_objErrorListProvider.Refresh();
            }
            catch (Exception)
            {
            }
        }
    }
}
